package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.pojo.UcenterMember;
import com.atguigu.educenter.pojo.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate <String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        //获取登录的手机号（如果是用qq邮箱的话就是邮箱号）
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码的非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw  new GuliException(20001,"登陆失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null){
            //数据库种没有手机号
            throw new GuliException(20001,"登录失败");

        }

        //判断密码是否正确
        //把输入的密码进行加密，然后在和数据库的密码进行比较
        //MD5加密
        String encrypt = MD5.encrypt(password);

        if (!encrypt.equals(mobileMember.getPassword())){
            throw new GuliException(20001,"登陆失败");
        }
        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登陆失败");

        }

        //生成token值
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;

    }

    @Override
    public void register(RegisterVo registerVo) {


        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//用户昵称
        String password = registerVo.getPassword();//用户密码

        //判断值是否为空
        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            //判断是否为空
            throw new GuliException(20001,"注册失败");

        }
        //判断验证码是否是正确
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败");
        }
        //判断手机号是否重复，存在相同的不能进行添加
        QueryWrapper<UcenterMember> wrappe= new QueryWrapper<>();
        wrappe.eq("mobile",mobile);
        //手机号进行查询
        Integer integer = baseMapper.selectCount(wrappe);
        if (integer>0){
            //有相同
            throw new GuliException(20001,"注册失败");
        }

        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(registerVo,member);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1C" +
                "onecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);

    }
//根据openId进行查询
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        //根据天数查询
        return baseMapper.countRegisterDay(day);

    }
}
